package cn.haohaowo.springmvc.view;

import java.util.Locale;
import java.util.Map;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

public class MixedViewResolver implements ViewResolver {
	
	private Map<String, ViewResolver> resolvers;
	
	public Map<String, ViewResolver> getResolvers() {
		return resolvers;
	}
	
	public void setResolvers(Map<String, ViewResolver> resolvers) {
		this.resolvers = resolvers;
	}

	@Override
	public View resolveViewName(String viewName, Locale locale)
			throws Exception {
		int n = viewName.lastIndexOf(".");
		ViewResolver resolver = null;
		if(-1 != n) {
			String suffix = viewName.substring(n + 1);
			resolver = resolvers.get(suffix);
			if(null == resolver) {
				throw new RuntimeException("No ViewResolver for " + suffix);
			}
			
		} else {
			resolver = resolvers.get("jsp");
			viewName += ".jsp";
		}
		return resolver.resolveViewName(viewName, locale);
	}

}
